﻿using LD.PLC;
using System;
using System.Linq;

namespace LD.Common
{
    /// <summary>
    /// Byte转换扩展
    /// </summary>
    public static class BitConvertEx
    {
        public static void DemoMW()
        {
            var opResult = PLCFactory.Instance.Read("6003", 8);
            if (opResult.IsSuccess)
            {
                var str1 = opResult.Content.ToStr(0, 4);
                var float1 = opResult.Content.ToFloat(6);
                var short1 = opResult.Content.ToInt16(10);
                var int1 = opResult.Content.ToInt32(12);
            }
        }
        public static void DemoMD()
        {
            //14000=plc中MD7000地址，因是双字所以*2
            //md地址不存在short
            var opResult = PLCFactory.Instance.Read("14000", 30);
            if (opResult.IsSuccess)
            {
                var int2 = opResult.Content.ToInt32(0);
                var int1 = opResult.Content.ToInt32(4);
                var float1 = opResult.Content.ToFloat(8);
                var str1 = opResult.Content.ToStr(12, 14);
            }
        }
        public static short ToInt16(this byte[] data, int startIndex)
        {
            if (data == null || data.Count() == 0)
                return 0;

            var _data = data.Skip(startIndex).Take(2).ToArray();
            return BitConverter.ToInt16(_data.Reverse().ToArray(), 0);
        }
        public static int ToInt32(this byte[] data, int startIndex)
        {
            if (data == null || data.Count() == 0)
                return 0;
            //CDAB
            var _data = data.Skip(startIndex).Take(4).ToArray();
            _data = new byte[4] { _data[2], _data[3], _data[0], _data[1] };
            return BitConverter.ToInt32(_data.Reverse().ToArray(), 0);
        }
        public static float ToFloat(this byte[] data, int startIndex)
        {
            if (data == null || data.Count() == 0)
                return 0;
            //CDAB
            var _data = data.Skip(startIndex).Take(4).ToArray();
            _data = new byte[4] { _data[2], _data[3], _data[0], _data[1] };
            return BitConverter.ToSingle(_data.Reverse().ToArray(), 0);
        }
        /// <summary>
        /// 转换为字符串
        /// </summary>
        /// <param name="data">byte数据</param>
        /// <param name="startIndex">截取起始索引</param>
        /// <param name="length">字符串长度</param>
        /// <returns></returns>
        public static string ToStr(this byte[] data, int startIndex, int length)
        {
            if (data == null || data.Count() == 0)
                return "";
            //CDAB to reverse
            //BADC not reverse
            var _data = data.Skip(startIndex).Take(length).ToArray();
            int f = _data.Length / 4;
            int y = _data.Length % 4;
            var bytes = new byte[length];

            for (int i = 0; i < f; i++)
            {
                bytes[i * 4] = _data[i * 4 + 1];
                bytes[i * 4 + 1] = _data[i * 4 + 0];
                bytes[i * 4 + 2] = _data[i * 4 + 3];
                bytes[i * 4 + 3] = _data[i * 4 + 2];
            }

            if (y > 0)
            {
                bytes[length - 1] = _data[length - 2];
                bytes[length - 2] = _data[length - 1];
            }

            return System.Text.ASCIIEncoding.ASCII.GetString(bytes.ToArray()).Replace("\0", "");
        }
    }
}
